package com.ljf.common.utils;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljf.common.model.BasisModel;
import com.ljf.common.validation.SelectLike;

import java.lang.reflect.Field;
import java.util.Date;

/**
 * Wrapper工具类
 *
 * @author LJF
 * @since 2024-01-26
 */
public class WrapperUtil {

    private WrapperUtil() {

    }

    /**
     * 填充查询条件
     *
     * @param model
     * @return
     * @throws IllegalAccessException
     */
    public static <E> QueryWrapper<E> getCondition(E model) throws IllegalAccessException {
        QueryWrapper<E> wrapper = new QueryWrapper<>();
        Field[] fields1 = model.getClass().getDeclaredFields();
        Field[] fields2 = model.getClass().getSuperclass().getDeclaredFields();
        Field[] fields = new Field[fields1.length + fields2.length];
        System.arraycopy(fields1, 0, fields, 0, fields1.length);
        System.arraycopy(fields2, 0, fields, fields1.length, fields2.length);
        for (Field field : fields) {
            field.setAccessible(true);
            TableField annotationColumn = field.getAnnotation(TableField.class);
            if (annotationColumn != null && StringUtil.isNotEmpty(annotationColumn.value())) {
                if (checkAnnotation(SelectLike.class, field)) {
                    fillLikeParam(wrapper, annotationColumn.value(), field.get(model));
                }
                else {
                    fillEqParam(wrapper, annotationColumn.value(), field.get(model));
                }
            }
        }
        return wrapper;
    }

    /**
     * 填充空的查询条件
     *
     * @return
     */
    public static <E> QueryWrapper<E> getEmptyWrapper() {
        QueryWrapper<E> wrapper = new QueryWrapper<>();
        wrapper.eq(BasisModel.COL_STATE, 2);
        return wrapper;
    }

    /**
     * 填充参数 相等
     *
     * @param wrapper
     * @param columnName
     * @param value
     */
    private static <E> void fillEqParam(QueryWrapper<E> wrapper, String columnName, Object value) {
        if (value instanceof String) {
            wrapper.eq(StringUtil.isNotEmpty((String) value), columnName, value);
            return;
        }
        else if (value instanceof Date) {
            wrapper.eq(columnName, DateUtil.format((Date) value));
            return;
        }
        wrapper.eq(value != null, columnName, value);
    }

    /**
     * 填充参数 模糊查询
     *
     * @param wrapper
     * @param columnName
     * @param value
     * @param <E>
     */
    private static <E> void fillLikeParam(QueryWrapper<E> wrapper, String columnName, Object value) {
        if (value instanceof String) {
            wrapper.like(StringUtil.isNotEmpty((String) value), columnName, value);
            return;
        }
        wrapper.like(value != null, columnName, value);
    }

    /**
     * 校验是否含有特定注解
     *
     * @param annotation
     * @param field
     * @return
     */
    private static boolean checkAnnotation(Class annotation, Field field) {
        if (field != null) {
            return field.isAnnotationPresent(annotation);
        }
        return false;
    }

}
